package com.ruoyi.project.mahjong.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.http.HttpUtilsBody;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.mahjong.domain.uyil.DeskUtils;
import com.ruoyi.project.mahjong.domain.vo.TableVo;
import com.ruoyi.project.mahjong.service.ITOpenTableService;
import com.ruoyi.project.system.domain.SysUser;
import io.netty.util.internal.ObjectUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.TTablenumber;
import com.ruoyi.project.mahjong.service.ITTablenumberService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 桌号Controller
 *
 * @author ruoyi
 * @date 2023-07-18
 */
@RestController
@RequestMapping("/mahjong/tablenumber")
public class TTablenumberController extends BaseController {
    @Autowired
    private ITTablenumberService tTablenumberService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ITOpenTableService openTableService;

    /**
     * 查询桌号列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:tablenumber:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTablenumber tTablenumber) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tTablenumber.setBelong(shopId.toString());
        startPage();
        List<TTablenumber> list = tTablenumberService.selectTTablenumberList(tTablenumber);
        return getDataTable(list);
    }


    @GetMapping("/listAll")
    public AjaxResult listAll(TTablenumber tTablenumber) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tTablenumber.setBelong(shopId.toString());
        List<TTablenumber> list = tTablenumberService.selectTTablenumberList(tTablenumber);
        return AjaxResult.success(list);
    }

    /**
     * 导出桌号列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:tablenumber:export')")
    @Log(title = "桌号", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TTablenumber tTablenumber) {
        List<TTablenumber> list = tTablenumberService.selectTTablenumberList(tTablenumber);
        ExcelUtil<TTablenumber> util = new ExcelUtil<TTablenumber>(TTablenumber.class);
        return util.exportExcel(list, "tablenumber");
    }

    /**
     * 获取桌号详细信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:tablenumber:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tTablenumberService.selectTTablenumberById(id));
    }

    /**
     * 新增桌号
     */
    @PreAuthorize("@ss.hasPermi('mahjong:tablenumber:add')")
    @Log(title = "桌号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTablenumber tTablenumber) {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tTablenumber.setBelong(shopId.toString());
        return toAjax(tTablenumberService.insertTTablenumber(tTablenumber));
    }

    @Log(title = "桌号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTablenumber tTablenumber)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tTablenumber.setBelong(shopId.toString());
        return toAjax(tTablenumberService.updateTTablenumber(tTablenumber));
    }

    /**
     * 修改桌号
     */
    @PreAuthorize("@ss.hasPermi('mahjong:tablenumber:edit')")
    @Log(title = "桌号", businessType = BusinessType.UPDATE)
    @PutMapping("/open")
    public AjaxResult open(@RequestBody TTablenumber tTablenumber) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        String result = redisCache.getCacheObject(Constants.SHOP_POWER_TOKEN);
        tTablenumber = tTablenumberService.selectTTablenumberById(tTablenumber.getId());
        if (ObjectUtils.isNull(result)) {
            result = HttpUtilsBody.getToken(DeskUtils.creatUrl, null);
            redisCache.setCacheObject(Constants.SHOP_POWER_TOKEN, result, 50, TimeUnit.MINUTES);
        }
        Map params = new HashMap(1 << 1);//用于存放request body
        params.put("unid", tTablenumber.getSn());
        params.put("io", tTablenumber.getLo());
        tTablenumber.setIsEnergize("1");
        try {
            HttpUtilsBody.postHttpUrl(DeskUtils.openUrl, result, params);
        } catch (IOException e) {
            e.printStackTrace();
        }

        tTablenumber.setBelong(shopId.toString());
        return toAjax(tTablenumberService.updateTTablenumber(tTablenumber));
    }

    /**
     * 修改桌号
     */
    @PreAuthorize("@ss.hasPermi('mahjong:tablenumber:edit')")
    @Log(title = "桌号", businessType = BusinessType.UPDATE)
    @PutMapping("/close")
    public AjaxResult close(@RequestBody TTablenumber tTablenumber) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        String result = redisCache.getCacheObject(Constants.SHOP_POWER_TOKEN);
        tTablenumber = tTablenumberService.selectTTablenumberById(tTablenumber.getId());
        if (ObjectUtils.isNull(result)) {
            result = HttpUtilsBody.getToken(DeskUtils.creatUrl, null);
            redisCache.setCacheObject(Constants.SHOP_POWER_TOKEN, result, 50, TimeUnit.MINUTES);
        }
        Map params = new HashMap(1 << 1);//用于存放request body
        params.put("unid", tTablenumber.getSn());
        params.put("io", tTablenumber.getLo());
        //关闭
        String falg = "false".equals(tTablenumber.getStatus()) ? "1" : "0";
        tTablenumber.setIsEnergize("0");
        try {
            HttpUtilsBody.postHttpUrl(DeskUtils.closeUrl, result, params);
        } catch (IOException e) {
            e.printStackTrace();
        }
        tTablenumber.setBelong(shopId.toString());
        return toAjax(tTablenumberService.updateTTablenumber(tTablenumber));
    }

    /**
     * 删除桌号
     */
    @PreAuthorize("@ss.hasPermi('mahjong:tablenumber:remove')")
    @Log(title = "桌号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tTablenumberService.deleteTTablenumberByIds(ids));
    }


    /**
     * 充值桌子
     */
    @PreAuthorize("@ss.hasPermi('mahjong:tablenumber:reset')")
    @Log(title = "桌号", businessType = BusinessType.DELETE)
    @GetMapping("/restTablenumber/{ids}")
    public AjaxResult reset(@PathVariable Long ids) {

        return toAjax(tTablenumberService.reset(ids));
    }


    @GetMapping("/tabList")
    public AjaxResult tableList() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        List<TTablenumber> tableVoList=   tTablenumberService.tableList(shopId);

        return AjaxResult.success(tableVoList);
    }



    @GetMapping("/tmpStopDesk/{orderSn}")
    public AjaxResult tmpStopDesk(@PathVariable String orderSn) {

        openTableService.tmpStopDesk(orderSn);

        return AjaxResult.success();
    }
}
